/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.content.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.content.ContentAttachDao;
import org.grayrabbit.cms.entity.ContentAttach;
import org.grayrabbit.cms.vo.content.ContentAttachBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>ContentAttach</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 14:40:08 
 */
@Repository
public class ContentAttachDaoImpl extends GenericHibernateDao<ContentAttach, String>
		implements ContentAttachDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From ContentAttach as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see ContentAttachDao#assembly(ContentAttachBean)
	 */
	public QueryCondition assembly(ContentAttachBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		qc.append("order by obj.attachId desc");
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see ContentAttachDao#assemblyCount(ContentAttachBean)
	 */
	public QueryCondition assemblyCount(ContentAttachBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see ContentAttachDao#count(ContentAttachBean)
	 */
	public int count(ContentAttachBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see ContentAttachDao#assemblyNameQuery(ContentAttachBean)
	 */
	public QueryCondition assermblyNameQuery(ContentAttachBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see ContentAttachDao#assemblyNameQueryCount(ContentAttachBean)
	 */
	public QueryCondition assemblyNameQueryCount(ContentAttachBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see ContentAttachDao#findByPage(Page, ContentAttachBean)
	 */
	public List<ContentAttach> findByPage(Page<ContentAttach> page,
			ContentAttachBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	
	/**
	 * 查询所有
	 * @param bean
	 * @return
	 */
	public List<ContentAttach> findContentAttch(ContentAttachBean bean){
		QueryCondition qc = assembly(bean);
		return findByQc(qc);
	}
	
	/**
	 * 获取自动生成的3套附件
	 * @param str
	 * @return
	 */
	public List<ContentAttach> findAutoComplateAttach(String str){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(str)){
			qc.add("And obj.attachName like ?", "%"+str+"%");
			qc.append("order by obj.attachId desc");
			return findByQc(qc);
		}else{
			return null;
		}
	}
	
	// 业务查询主要逻辑代码
	private void transNameQuery(ContentAttachBean bean,QueryCondition qc){
		if(bean!=null){
			if(bean.getAttachName() != null){
				qc.add("And obj.attachName = ? ", bean.getAttachName()+"%");
			}
            if(bean.getAttachType() != null){
				qc.add("And obj.attachType like ? ", bean.getAttachType()+"%");
			}
            if(bean.getAttachExt() != null){
				qc.add("And obj.attachExt like ? ", bean.getAttachExt()+"%");
			}
            if(bean.getAttachUrl() != null){
				qc.add("And obj.attachUrl like ? ", bean.getAttachUrl()+"%");
			}
            if(bean.getAttachSize() != null){
				qc.add("And obj.attachSize = ? ", bean.getAttachSize());
			}
            if(bean.getAttachMemo() != null){
				qc.add("And obj.attachMemo like ? ", bean.getAttachMemo()+"%");
			}
            if(bean.getAttachDownloadNum() != null&&bean.getContent()==null){
				qc.add("And obj.attachDownloadNum = ? ", bean.getAttachDownloadNum());
			}
            if(!(bean.getAttachDownloadNum() != null)&&bean.getContent()!=null&&StringUtils.isNotBlank(bean.getContent().getContentId())){
            	qc.add("And obj.content.contentId = ? ", bean.getContent().getContentId());
            }
            if(bean.getAttachDownloadNum() != null&&bean.getContent()!=null&&StringUtils.isNotBlank(bean.getContent().getContentId())){
            	qc.add("And obj.attachDownloadNum = ? ", bean.getAttachDownloadNum());
            	qc.add("Or obj.content.contentId = ? ", bean.getContent().getContentId());
            }
		}
	}
	/**
	 * 根据文章Id查询上传文件路径
	 * @return
	 */
	public List<ContentAttach> ftlFindContentImg(String contentId,int num){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.content.contentId = ? ", contentId);
		if(num != 0){
			qc.append(" And rownum<="+num);
		}
		return findByQc(qc);
	}

	public List<ContentAttach> findContentAttaches(String contentId) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.content.contentId = ? ", contentId);
		return findByQc(qc);
	}

	public List<ContentAttach> loadContentAttaches(String contentId, String type) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.content.contentId = ? ", contentId);
		qc.add(" And obj.attachType = ? ", type);
		qc.appenOrderBy(" obj.focusPic desc");
		return findByQc(qc);
	}

	public List<ContentAttach> loadAttachesExpPic(String contentId,
			String[] sufs) {
		if(StringUtils.isBlank(contentId))  return new ArrayList<ContentAttach>();
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.content.contentId = ? ", contentId);
		if(!ArrayUtils.isEmpty(sufs) ){
			qc.append(" and ( ");
			int i = sufs.length;
			for(String s : sufs){
				i--;
				qc.add("  obj.attachExt != ? ", s.trim());
				if(i != 0){
					qc.append(" and ");
				}
			}		
			qc.append(" ) "); 
		}
		qc.appenOrderBy(" obj.attachExt desc "); 
		return findByQc(qc);
	}
	public List<ContentAttach> loadAttachesExpPic2(String contentId) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.content.contentId = ? ", contentId);
		qc.appenOrderBy(" obj.attachExt desc "); 
		return findByQc(qc);
	}

}